package com.module.boots.api.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.module.boots.exception.CommonRuntimeException;


/**
 * http工具类
 * @author:溪云阁
 * @date:2020年5月2日
 */
public class HttpContextUtils {

    public static HttpServletRequest getHttpServletRequest() {
        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getRequest();
        } else {
            throw new CommonRuntimeException("获取不到当前的requestAttributes");
        }
    }

    public static HttpServletResponse getHttpServletResponse() {
        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (requestAttributes != null) {
            return requestAttributes.getResponse();
        } else {
            throw new CommonRuntimeException("获取不到当前的requestAttributes");
        }
    }
}
